package com.wanxi.dangdang.controller.front.scenic;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.model.pojo.Scenic;
import com.wanxi.dangdang.model.vo.*;
import com.wanxi.dangdang.service.ScenicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/front/b/science")
@Api(tags = "B端页面景区")
@Slf4j
public class BScenicController {
    @Autowired
    private ScenicService scenicService;

    /**
     * @Author: yanguangqiong
     * @Description: 通过景区id获取景区信息，传入景区id，返回景区model
     * @Date: 14:15 2021/8/30
     */

    @ApiOperation("分销商通过景区id查看景区信息")
    @GetMapping("/findById")
    public CommonResult<ScenicFindByIdVO> findById(@ApiParam("景区Id") @RequestParam Long id) {
        log.info("景区id为：{}", id);
        if (id==null) {
            return CommonResult.validateFailed();
        }
        ScenicFindByIdVO scenic = scenicService.findById(id);
        return CommonResult.successData(scenic);
    }

    /**
     * @Author: yanguangqiong
     * @Description: 分页查询找到所有景区信息，包括模糊查询
     * @Date: 14:25 2021/8/30
     */

    @ApiOperation("分页查询找到所有景区信息，包括模糊查询")//首页做成es
    @GetMapping("/findPage") //C端用户传入分销商id 地区 景区标题
    public CommonResult<PageInfo<ScenicPageVO>> findPage(PageScenicDimVO pageScenicDimVO, Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null) {
            log.info("页数为：{}", pageNum, "每页为：{}", pageSize);
            return CommonResult.validateFailed();
        }
        PageInfo<ScenicPageVO> pageInfo = scenicService.findPage(pageScenicDimVO, pageNum, pageSize);
        return CommonResult.successData(pageInfo);
    }

    @ApiOperation("首页分页查询")
    @GetMapping("/findHomePage")
    public CommonResult<Page<Scenic>> findHomePage(Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null) {
            log.info("页数为：{}", pageNum, "每页为：{}", pageSize);
            return CommonResult.validateFailed();
        }
        scenicService.saveAll();
        Page<Scenic> page = scenicService.findHomePage(pageNum, pageSize);
        return CommonResult.successData(page);
    }

    //首页es高亮
    @ApiOperation("首页查询关键字")
    @PostMapping("/findTitle")
    public Object findByNameHighLight(@RequestParam String keyword, Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null) {
            log.info("页数为：{}", pageNum, "每页为：{}", pageSize);
            return CommonResult.validateFailed();
        }
        SearchHits<Scenic> highLight = scenicService.findByNameHighLight(keyword, pageNum, pageSize);
        return CommonResult.success(highLight);
    }


    /**
     * @Author: yanguangqiong
     * @Description: 通过分销商id找到分销商店铺景区信息，包括模糊查询和分页
     * @Date: 14:25 2021/8/30
     */
    @ApiOperation("通过分销商id找到分销商店铺景区信息，包括模糊查询和分页,需要分销商登录")//分销商id和景区标题
    @GetMapping("/findByDistributorId")
    public CommonResult<PageInfo<List<BUserScenicVO>>> findByDistributorId(HttpSession httpSession, PageScenicDimVO pageScenicDimVO, Integer pageNum, Integer pageSize) {
        if (pageNum==null || pageSize==null) {
            log.info("页数为：{}", pageNum, "每页为：{}", pageSize);
            return CommonResult.validateFailed();
        }
        Long id = (Long) httpSession.getAttribute("id");
        pageScenicDimVO.setId(id);
        PageInfo<List<BUserScenicVO>> pageInfo = scenicService.findByDistributorId(pageScenicDimVO, pageNum, pageSize);
        return CommonResult.successData(pageInfo);
    }

    /**
     * @Author: yanguangqiong
     * @Description: 更改分销商店铺中的景区状态
     * @Date: 14:37 2021/8/30
     */

    @ApiOperation("更改分销商店铺中的景区状态，需要分销商登录")
    @PostMapping("/updateStatus")
    public CommonResult updateStatus(HttpSession httpSession, @ApiParam("景区Id") @RequestBody ScenicIdVO scenicIdVO) {
        Long scenicId = scenicIdVO.getId();
        if (scenicId==null) {
            log.info("景区id为" +
                    "：{}", scenicId);
            return CommonResult.validateFailed();
        }
        Long id = (Long) httpSession.getAttribute("id");
        scenicService.updateStatus(id, scenicId);
        return CommonResult.success();
    }

    /**
     * @Author: yanguangqiong
     * @Description:
     * @Date: 13:57 2021/8/31
     */
    @ApiOperation("分销商对自己的店铺的景区的移除操作，需要分销商登录")
    @PostMapping("/delete")
    public CommonResult deleteScenicByBUser(HttpSession httpSession, @ApiParam("景区Id") @RequestBody ScenicIdVO scenicIdVO) {
        Long scenicId = scenicIdVO.getId();
        if (scenicId==null) {
            log.info("景区id为：{}", scenicId);
            return CommonResult.validateFailed();
        }
        Long id = (Long) httpSession.getAttribute("id");
        scenicService.deleteScenicByBUser(id, scenicId);
        return CommonResult.success();
    }

    /**
     * @Author: yanguangqiong
     * @Description: 添加景点到分销商的店铺
     * @Date: 14:37 2021/8/30
     */
    @ApiOperation("添加景点到分销商的店铺,返回值为error时表示未添加到我的店铺，需要分销商登录")
    @PostMapping("/saveToMyStore")
    public CommonResult saveToMyStore(HttpSession httpSession, @ApiParam("景区Id") @RequestBody ScenicIdVO scenicIdVO) {
        Long scenicId = scenicIdVO.getId();
        if (scenicId==null) {
            log.info("景区id为：{}", scenicId);
            return CommonResult.validateFailed();
        }
        Long id = (Long) httpSession.getAttribute("id");
        String result = scenicService.saveToMyStore(id, scenicId);
        return CommonResult.success(result);
    }

    /**
     * @Author: yanguangqiong
     * @Description: 首页平台推荐佣金最高前5
     * @Date: 15:17 2021/8/30
     */
    @ApiOperation("首页平台推荐佣金最高前5")
    @GetMapping("/PlatformRecommend")
    public CommonResult<PageInfo<List<ScenicPageVO>>> PlatformRecommend() {
        PageInfo<List<ScenicPageVO>> pageInfo = scenicService.PlatformRecommend();
        return CommonResult.successData(pageInfo);
    }

    @ApiOperation("避免重复添加到我的店铺")
    @GetMapping("/findRepetitionStore")
    public CommonResult findRepetitionStore(HttpSession httpSession,@ApiParam("景区Id") @RequestParam Long id) {
//        Long scenicId = scenicIdVO.getId();
        if (id ==null) {
            log.info("景区id为：{}", id);
            return CommonResult.validateFailed();
        }
        Long distributorId = (Long) httpSession.getAttribute("id");
       String result= scenicService.findRepetitionStore(distributorId,id);
       return CommonResult.success(result);
    }
}
